package edu.ccut.titan.tag.chart.render;

import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.util.List;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import edu.ccut.titan.tag.chart.utils.ChartInfo;

/**
 * 图表渲染抽象积类
 * 
 * @author LeslieGu
 */
public abstract class ChartRender {
	
    public static final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 18);
    public static final Font DEFAULT_SUBTITLE_FONT = new Font("SansSerif", Font.BOLD, 12);
    public static final Font DEFAULT_LEGEND_FONT = new Font("SansSerif", Font.PLAIN, 12);
    public static final Font DEFAULT_NODATA_FONT = new Font("SansSerif", Font.BOLD, 30);


	public CategoryLabelPositions getCategoryLablePositions(ChartInfo chartInfo) {
		String position = chartInfo.getCategoryAxislablePosition();
		if (position != null && position.length() > 0) {
			if ("UP_90".equalsIgnoreCase(position)) {
				return CategoryLabelPositions.UP_90;
			}
			
			if ("UP_45".equalsIgnoreCase(position)) {
				return CategoryLabelPositions.UP_45;
			}
			
			if ("DOWN_45".equalsIgnoreCase(position)) {
				return CategoryLabelPositions.DOWN_45;
			}
			
			if ("DOWN_90".equalsIgnoreCase(position)) {
				return CategoryLabelPositions.DOWN_90;
			}
		}
		
		return CategoryLabelPositions.STANDARD;
	}

	
    protected void setNoData(Plot plot) {
        plot.setNoDataMessage("没有数据返回");
        plot.setNoDataMessageFont(DEFAULT_NODATA_FONT);
        plot.setNoDataMessagePaint(Color.red);
    }

    protected void renderChart(JFreeChart chart, ChartInfo chartInfo) {
        Plot plot = renderPrivateChart(chart, chartInfo);

        setNoData(plot);
        setSubTitle(chart, chartInfo);
        setFont(chart, chartInfo);
        renderFont(chart, chartInfo);
        renderNakedChart(chart, chartInfo);
    }
    
    
    public final JFreeChart buildChart(ChartInfo info) {
    	JFreeChart chart = this.createChart(info);
    	this.renderChart(chart, info);
    	
    	return chart;
    }
    
    /**
     * 对图表进行渲染
     * 
     * @param chart
     * @param chartInfo
     * @return
     */
    protected abstract Plot renderPrivateChart(JFreeChart chart, ChartInfo chartInfo);

    /**
     * 创建图表
     * @param chartInfo
     * @return
     */
    protected abstract JFreeChart createChart(ChartInfo chartInfo);
    
	protected CategoryDataset getCategoryDataset(List<List<?>> datas) {
		DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();

		if (datas != null) {
			for (List<?> data : datas) {
				if (data.size() > 2) {
					try {
						categoryDataset.addValue((Number) data.get(0),
								(Comparable<?>) data.get(1),
								(Comparable<?>) data.get(2));
					} catch (RuntimeException e) {
						e.printStackTrace();
					}
				}
			}
		}

		return categoryDataset;
	}
	
    protected void setSubTitle(JFreeChart chart, ChartInfo chartInfo) {
        if (null != chartInfo.getSubTitle()) {
        	chart.addSubtitle(new TextTitle(chartInfo.getSubTitle(), DEFAULT_SUBTITLE_FONT));
        }
    }

    protected void setFont(JFreeChart chart, ChartInfo chartInfo) {
        if (null != chartInfo) {
            chart.getTitle().setFont(DEFAULT_TITLE_FONT);
            chart.setBackgroundPaint(Color.white);
            //Legend
            if (chartInfo.isLegend()) {
                chart.getLegend().setItemFont(DEFAULT_LEGEND_FONT);
            }
        }
    }

    /**
     * 控制字体是否削除锯齿
     *
     * @param chartInfo
     */
    protected void renderFont(JFreeChart chart, ChartInfo chartInfo) {
        if (null != chartInfo) {
            chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        }
    }

    protected void renderNakedChart(JFreeChart chart, ChartInfo chartInfo) {
        if (null != chartInfo && chartInfo.isNaked()) {
            chart.clearSubtitles();
            chart.setTitle("");
        }
    }
}
